package com.jack006.article.html;

import com.jack006.api.config.RabbitMQConfig;
import com.jack006.article.html.controller.ArticleHtmlComponent;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Classname RabbitMQConsumer
 * @Description
 * @Date 2021-07-28 22:06
 * @Created by jack
 */
@Component
public class RabbitMQConsumer {

    @Autowired
    private ArticleHtmlComponent articleHtmlComponent;

    @RabbitListener(queues = {RabbitMQConfig.QUEUE_DOWNLOAD_HTMl})
    public void watchQueue(String payload, Message message) {

        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        if (routingKey.equalsIgnoreCase("article.publish.download.do")) {
            System.out.println("article.publish.download.do");
        } else if(routingKey.equalsIgnoreCase("article.delete.do")) {
            try {
                articleHtmlComponent.delete(payload);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(routingKey.equalsIgnoreCase("article.download.do")) {
            String[] split = payload.split(",");
            String articleId = split[0];
            String articleMongoId = split[1];
            try {
                articleHtmlComponent.download(articleId,articleMongoId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("不符合的规则："+routingKey);
        }
    }
}
